package com.yuven.baselib.utils;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import kotlin.reflect.KClass;

/**
 * 
 *
 * @author :        Yuven
 * @date :          2022/7/8 5:12 下午
 * @desc :
 */
public class ActivityManager implements Application.ActivityLifecycleCallbacks {
    private Application mApplication = null;
    private List<Activity> mActivitys = new ArrayList<>();

    private ActivityManager() {
    }

    private static class InstanceHolder {
        private static ActivityManager sActivityManager = new ActivityManager();
    }

    public static ActivityManager getInstance() {
        return InstanceHolder.sActivityManager;
    }


    /**
     * 结束所有页面，保留当前页面
     *
     * @param currentAc
     */
    public void finishActivityUntilThis(Activity currentAc) {
        synchronized (mActivitys) {
            for (int i = 0; i < mActivitys.size(); i++) {
                Activity activity = mActivitys.get(i);
                if (!currentAc.getClass().getSimpleName().equals(
                        activity.getClass().getSimpleName())) {
                    mActivitys.remove(activity);
                    activity.finish();
                    i--;
                }
            }
        }
    }

    public void finishActivityUntilThis(@NotNull Class<?> acSimpleName) {
        synchronized (mActivitys) {
            for (int i = 0; i < mActivitys.size(); i++) {
                Activity activity = mActivitys.get(i);
                if (!acSimpleName.getSimpleName().equals(
                        activity.getClass().getSimpleName())) {
                    mActivitys.remove(activity);
                    activity.finish();
                    i--;
                }
            }
        }
    }

    public void finishAllActivity() {
        synchronized (mActivitys) {
            for (int i = 0; i < mActivitys.size(); i++) {
                Activity activity = mActivitys.get(i);
                mActivitys.remove(activity);
                activity.finish();
                i--;
            }
        }
    }

    public boolean isActivityExist(@NotNull Class<?> acSimpleName) {
        synchronized (mActivitys) {
            for (int i = 0; i < mActivitys.size(); i++) {
                Activity activity = mActivitys.get(i);
                if (acSimpleName.getSimpleName().equals(
                        activity.getClass().getSimpleName())) {
                    return true;
                }
            }
            return false;
        }
    }

    public void init(Application application) {
        mApplication = application;
        mApplication.registerActivityLifecycleCallbacks(this);
    }

    public Activity getTopActivity() {
        return mActivitys.get(mActivitys.size() - 1);
    }

    private void pushActivity(Activity activity) {
        synchronized (mActivitys) {
            mActivitys.add(activity);
        }
    }

    private void popActivity(Activity activity) {
        synchronized (mActivitys) {
            mActivitys.remove(activity);
        }
    }

    @Override
    public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {
        pushActivity(activity);
        Logger.i("ActivityManager", "onActivityCreated -> " + activity.getClass().getName());
    }

    @Override
    public void onActivityStarted(@NonNull Activity activity) {
        Logger.i("ActivityManager", "onActivityStarted -> " + activity.getClass().getName());
    }

    @Override
    public void onActivityResumed(@NonNull Activity activity) {
        Logger.i("ActivityManager", "onActivityResumed -> " + activity.getClass().getName());
    }

    @Override
    public void onActivityPaused(@NonNull Activity activity) {
        Logger.i("ActivityManager", "onActivityPaused -> " + activity.getClass().getName());
    }

    @Override
    public void onActivityStopped(@NonNull Activity activity) {
        Logger.i("ActivityManager", "onActivityStopped -> " + activity.getClass().getName());
    }

    @Override
    public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {
        Logger.i("ActivityManager", "onActivitySaveInstanceState -> " + activity.getClass().getName());
    }

    @Override
    public void onActivityDestroyed(@NonNull Activity activity) {
        popActivity(activity);
        Logger.i("ActivityManager", "onActivityDestroyed -> " + activity.getClass().getName());
    }
}
